<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Part extends CI_Controller {
    function Part() 
    {
        parent::__construct();
        $this->load->helper('html');
        $this->load->helper('url');
        $this->load->helper('form');
    }
	
    public function index()
    {
        $data['message'] = "";
        
        $data['make'] = $this->db->get('psacar_make');
        //$data['model'] = $this->db->get('psacar_model');

        $this->db->order_by("order", "asc"); 
        $this->db->order_by("name", "asc"); 
        $data['part'] = $this->db->get('psacar_part');

        $this->db->order_by("order", "asc"); 
        $this->db->order_by("province", "asc"); 
        $data['location'] = $this->db->get('psacar_location');
        
        $this->db->order_by('company', 'asc');
        $data['dealer'] = $this->db->get_where('psacar_member', array('type_id' => 3));
        
        $data['template_page'] = 'templates/part/part_search';
        
        $this->load->view('header', $data);
        $this->load->view('part', $data);
        $this->load->view('footer', $data);
    }
    
    public function detail()
    {
        $data['message'] = "";

        $part = $this->uri->segment(3);        
        if($part)
        {       
            $data['part_list'] = $this->db->get_where('psacar_part', array('name' => $part));
            /*
            $this->load->model('Dealer_model');
            $data['dealer_list'] = $this->Dealer_model->get_dealer_by_id($dealer_id);
            
            $this->load->model('Part_model');
            $data['service_list'] = '';
            foreach($data['dealer_list']->result() as $row)
            {                 
                $service_id_list = preg_split('/,/', $row->service_id, -1, PREG_SPLIT_OFFSET_CAPTURE);
                foreach($service_id_list as $a_service)
                {
                    if(strlen($a_service[0]) != 0)
                    {
                        $service_list = $this->Part_model->get_part_by_id($a_service[0]);
                        foreach($service_list->result() as $service_row)
                            $data['service_list'] = $data['service_list'] . $service_row->name . ',';
                    }
                }
            }
            
            if(substr($data['service_list'], strlen(',') * -1) == ',')
                $data['service_list'] = substr($data['service_list'], 0, strlen($data['service_list']) - 1);
            
            $data['car_list'] = $this->Dealer_model->get_car_by_dealer_id($dealer_id);
            */

            $this->load->view('header', $data);
            $this->load->view('templates/part/part_detail', $data);

            $data['make']= $this->db->get('psacar_make');
            $this->load->view('footer', $data); 
        }
        else
            redirect(base_url().'index.php/service/search');
    }
    
    public function result(){	
        $offset = $this->uri->segment(3);
        if(strlen($offset) == 0)
            $offset = 0;
        
        $limit = $this->input->post('limit');
        if(strlen($limit) == 0)
            $limit = 10;
        
        $part = $this->input->post('part');
        $location = $this->input->post('location');
        $vendor = $this->input->post('vendor');
        $make = $this->input->post('make');
        $model = $this->input->post('model');

        $data['criteria_list'] = 
            $this->register_search_criteria(                    
                $make, 
                $model, 
                $vendor, 
                $part, 
                $location);

        $this->load->model('Part_model');
        $part_list = $this->Part_model->get_part_list(
            $make, 
            $model, 
            $vendor,
            $part,
            $location);

        //Pagination        
        $data['pagination'] = $this->generate_pagination(
            'index.php/part/result',
            $part_list->num_rows,
            $limit);
        
        $part_list = array_slice($part_list->result(), $offset, $limit);
        $data['member_list'] = $part_list;
        $this->load->view('header', $data);
        $this->load->view('templates/member/member_search_result', $data);
        $data['make']= $this->db->get('psacar_make');
        $this->load->view('footer', $data);
    }
    
    public function register() {
        $data['message'] = "";
        $this->db->where('parent_id', 0);
        $data['part'] = $this->db->get('psacar_part');
        $data['make']= $this->db->get('psacar_make');
        $data['model']= $this->db->get('psacar_model');        
        $data['location']=$this->db->get('psacar_location');        

        $data['template_page'] = 'templates/part/part_register';

        $this->load->view('header', $data);
        $this->load->view('part', $data);
        $this->load->view('footer', $data);        
    }
    
    public function submit() {        
        if (!$this->session->userdata('validated')) {
            $data["message_title"] = "Authentication failure";
            $data["message_body"] = "Please login to list your product.";            
        }
        else if (($this->session->userdata('membership') != 1) && ($this->session->userdata('membership') != 3)) {
            $data["message_title"] = "Access denied";
            $data["message_body"] = "With the current role, you are not allowed to list any car.";
        }
        else {
            $member = $this->input->post('member');            
            $location = $this->input->post('location');

            $expiration_date = new DateTime(date('Y-m-d'));
            $expiration_date->add(new DateInterval('P30D'));            

            $part = 0;
            if(is_array($this->input->post('part'))){
                foreach ($this->input->post('part') as $a_part) 
                    $part = $a_part;
            }
            
            if(is_array($this->input->post('make'))){
                foreach ($this->input->post('make') as $a_make) {
                    $member_part =  array(
                        'member_id' => $member,
                        'part_id' => $part,
                        'location_id' => $location,
                        'make_id' => $a_make,
                        'status' => 0,
                        'expiration_date' => $expiration_date->format("Y-m-d"));

                    $this->load->database();                            
                    $this->db->insert('psacar_member_part', $this->security->xss_clean($member_part));
                    $member_part_id = $this->db->insert_id();

                    //Reference number
                    $ref_code = '';
                    $part_code_list = $this->db->get_where('psacar_part', array('id' => $part));
                    if ($part_code_list->num_rows != 0) {
                        $part_code = $part_code_list->row();
                        $ref_code = $part_code->code;
                    }

                    $ref_code .= 
                        substr('00000' . $member_part_id, -5) . 
                        random_string('alnum', 3);
                    $data['ref_code'] = strtoupper($ref_code);

                    $this->db->where('id', $member_part_id);
                    $this->db->update('psacar_member_part', $data);  
                }                                              
            }                        

            $data["message_title"] = "Product registered";
            $data["message_body"] = 
                "Thank you for listing with <a href='" . base_url() . "'>www.psacar.com.</a> Your listing will appear online once it has been approved by our admin team.";
        }
        
        $data['message'] = "";

        $this->load->view('header', $data);
        $this->load->view('information', $data);
        $data['make']= $this->db->get('psacar_make');
        $this->load->view('footer', $data);        
    }
    
    public function subcategory(){        
        $part_id = $this->uri->segment(3);
        echo "<option value=''>Please select</option>";
        if ($part_id)
        {
            $part_list = $this->db->get_where('psacar_part', array('parent_id' => $part_id));
            foreach ($part_list->result() as $part)
            {
                echo "<option value='" . $part->id . "' title='" . $part->description . "'>" . $part->name . "</option>";
            }
        }
    }        
        
    public function listed() {
        $data['message'] = "";
        
        $offset = $this->uri->segment(3);
        if(strlen($offset) == 0)
            $offset = 0;
        
        $limit = $this->input->post('limit');
        if(strlen($limit) == 0)
            $limit = 10;
        
        $member_id = $this->uri->segment(3);
        
        $this->load->model('Part_model');
        $part_list = $this->Part_model->get_approved_part_by_member_id($member_id);
        
        //Pagination        
        $data['pagination'] = $this->generate_pagination(
            'index.php/part/result',
            $part_list->num_rows,
            $limit);
        
        $part_list = array_slice($part_list->result(), $offset, $limit);
        $data['member_list'] = $part_list;        
        $this->load->view('header', $data);
        $this->load->view('templates/member/member_search_result', $data);
        $data['make']= $this->db->get('psacar_make');
        $this->load->view('footer',$data);         
    } 
   
    public function pending() {
        $data['message'] = "";
        
        $offset = $this->uri->segment(3);
        if(strlen($offset) == 0)
            $offset = 0;
        
        $limit = $this->input->post('limit');
        if(strlen($limit) == 0)
            $limit = 10;

        $member_id = $this->uri->segment(3);
        
        $this->load->model('Part_model');
        $part_list = $this->Part_model->get_pending_part_by_member_id($member_id);
        
        //Pagination        
        $data['pagination'] = $this->generate_pagination(
            'index.php/part/result',
            $part_list->num_rows,
            $limit);
        
        $part_list = array_slice($part_list->result(), $offset, $limit);
        $data['member_list'] = $part_list;        
        $this->load->view('header', $data);
        $this->load->view('templates/member/member_search_result', $data);
        $data['make']= $this->db->get('psacar_make');
        $this->load->view('footer',$data);                 
    }

    public function expired() {
        $data['message'] = "";
        
        $offset = $this->uri->segment(3);
        if(strlen($offset) == 0)
            $offset = 0;
        
        $limit = $this->input->post('limit');
        if(strlen($limit) == 0)
            $limit = 10;

        $member_id = $this->uri->segment(3);
        
        $this->load->model('Part_model');
        $part_list = $this->Part_model->get_expired_part_by_member_id($member_id);
        
        //Pagination        
        $data['pagination'] = $this->generate_pagination(
            'index.php/part/result',
            $part_list->num_rows,
            $limit);
        
        $part_list = array_slice($part_list->result(), $offset, $limit);
        $data['member_list'] = $part_list;
        
        $this->load->view('header', $data);
        $this->load->view('templates/member/member_search_result', $data);
        $data['make']= $this->db->get('psacar_make');
        $this->load->view('footer',$data);        
    }    
    
    public function generate_pagination($base_url = '', $total_row = 0, $per_page = 0) {       
        $this->load->library('pagination');
        $config['base_url'] = base_url() . $base_url;
        $config['total_rows'] = $total_row;
        $config['per_page'] = $per_page;

        $limiter = '<select name="limit">';
        for($counter = 10; $counter <= 100; ) {
            $limiter .= 
                '<option value="' . $counter . '" ' . 
                ($per_page == $counter ? 'selected' : '') . 
                '>' . $counter . ' per page</option>';
            $counter += 10;
        }
        $limiter .= "</select> | ";
                
        $config['full_tag_open'] = '<div class="pagination">' . $limiter;
        $config['full_tag_close'] = '</div>';
        $this->pagination->initialize($config);
        return $this->pagination->create_links();
    }    
    
    public function register_search_criteria(
        $make = '', 
        $model = '', 
        $vendor = '',
        $part = '',
        $location = '') {
        
        $search_critieria = array(
            array(
                'field' => 'make',
                'value' => $make),
            array(
                'field' => 'model',
                'value' => $model),
            array(
                'field' => 'vendor',
                'value' => $vendor),
            array(
                'field' => 'part',
                'value' => $part),
            array(
                'field' => 'location',
                'value' => $location),                      
        );

        return $search_critieria;
    }    
}